package com.wj.jiangan.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author WJ
 * 日志切面
 */
@Aspect
@Component
@Slf4j
public class LogAspect {

    /**
     * 添加业务逻辑方法切入点
     */
    @Pointcut("execution(*  com.wj.jiangan.service.impl.*.add*(..))")
    public void insertCell() {
    }

    /**
     * 修改业务逻辑方法切入点
     */
    @Pointcut("execution(*  com.wj.jiangan.service.impl.*.update*(..))")
    public void updateCell() {
    }

    /**
     * 删除业务逻辑方法切入点
     * delete*(..)
     */
    @Pointcut("execution(*  com.wj.jiangan.service.impl.*.delete*(..))")
    public void deleteCell() {
    }

    @Around("execution(* com.wj.jiangan.service.impl.*.*(..))")
    public Object callDurationAdvice(ProceedingJoinPoint pjp) throws Throwable {
        Signature signature = pjp.getSignature();
        Object[] args = pjp.getArgs();
        String argList = Arrays.toString(args);

        log.debug("{}.{}({}) started ", signature.getDeclaringTypeName(), signature.getName(), argList);
        long s = System.nanoTime();
        Object proceed = pjp.proceed(args);
        long e = System.nanoTime();

        log.debug("{}.{}({}) ended after {} ms", signature.getDeclaringTypeName(),
                signature.getName(), argList,
                ((double) (e - s) / 1000000));
        return proceed;
    }


    /**
     * 添加操作日志(后置通知)
     *
     * @param joinPoint
     * @param object
     */
    @AfterReturning(value = "insertCell()", argNames = "object", returning = "object")
    public void insertLog(JoinPoint joinPoint, Object object) throws Throwable {
        // Admin admin=(Admin)
        // request.getSession().getAttribute("businessAdmin");
        // 判断参数
        if (joinPoint.getArgs() == null) {// 没有参数
            return;
        }
        // 获取方法名
        String methodName = joinPoint.getSignature().getName();


    }

    /**
     * 管理员修改操作日志(后置通知)
     *
     * @param joinPoint
     * @param object
     * @throws Throwable
     */
    @AfterReturning(value = "updateCell()", argNames = "object", returning = "object")
    public void updateLog(JoinPoint joinPoint, Object object) throws Throwable {
        // Admin admin=(Admin)
        // request.getSession().getAttribute("businessAdmin");

        // 判断参数
        if (joinPoint.getArgs() == null) {// 没有参数
            return;
        }
        // 获取方法名
        String methodName = joinPoint.getSignature().getName();


    }

    /**
     * 删除操作
     *
     * @param joinPoint
     * @param object
     */
    @AfterReturning(value = "deleteCell()", returning = "object")
    public void deleteLog(JoinPoint joinPoint, Object object) throws Throwable {

        // 判断参数
        if (joinPoint.getArgs() == null) {// 没有参数
            return;
        }
        // 获取方法名
        String methodName = joinPoint.getSignature().getName();

        StringBuffer rs = new StringBuffer();
        rs.append(methodName);
        String className = null;
        for (Object info : joinPoint.getArgs()) {
            // 获取对象类型
            className = info.getClass().getName();
            className = className.substring(className.lastIndexOf(".") + 1);
            rs.append("[参数，类型:" + className + "，值:(id:"
                    + joinPoint.getArgs()[0] + ")");
        }


        log.debug(rs.toString());
    }


}